import React, { useState, useEffect, useRef, memo } from 'react';
import { Form, Input, Button, Alert, message } from 'antd';
import { UserOutlined, LockOutlined, ReadOutlined, WindowsOutlined, LineChartOutlined, StarFilled } from '@ant-design/icons';
import { validateCredentials, setAuthToken, isAuthenticated } from '@/utils/auth';
import { useNavigate } from 'react-router-dom';
import './index.scss';

function Login() {
  const [form] = Form.useForm();
  const navigate = useNavigate();
  const [loading, setLoading] = useState(false);

  const handleLogin = () => {
    form.validateFields().then(values => {
      setLoading(true);

      setTimeout(() => {
        const { username, password } = values;
        const result = validateCredentials(username, password);

        if (result.success) {
          setAuthToken(result.user);
          message.success('登录成功！');
          navigate('/');
        } else {
          message.error(result.message || '登录失败！');
        }

        setLoading(false);
      }, 1000);
    }).catch(() => {});
  };

  useEffect(() => {
    if (isAuthenticated()) {
      navigate('/');
    }
  }, [navigate]);

  return (
    <div className="login-container">
      <div className="login-bg">
        <div className="bg-overlay"></div>
      </div>

      <div className="login-content">
        <div className="login-card">
          <div className="login-header">
            <div className="login-icon">
              <ReadOutlined />
            </div>
            <h1>图书馆管理系统</h1>
            <p>智慧管理，高效服务</p>
          </div>

          <Form
            form={form}
            className="login-form"
            onFinish={handleLogin}
          >
            <Form.Item
              name="username"
              rules={[
                { required: true, message: '请输入用户名' },
                { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符' }
              ]}
            >
              <Input
                placeholder="请输入用户名"
                prefix={<UserOutlined />}
                size="large"
                allowClear
              />
            </Form.Item>

            <Form.Item
              name="password"
              rules={[
                { required: true, message: '请输入密码' },
                { min: 6, max: 20, message: '密码长度在 6 到 20 个字符' }
              ]}
            >
              <Input.Password
                placeholder="请输入密码"
                prefix={<LockOutlined />}
                size="large"
                allowClear
                onPressEnter={handleLogin}
              />
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                size="large"
                loading={loading}
                block
                htmlType="submit"
                className="login-button"
              >
                {loading ? '登录中...' : '立即登录'}
              </Button>
            </Form.Item>
          </Form>

          <div className="login-tips">
            <Alert
              message="演示账号"
              description={
                <div>
                  <p><strong>管理员：</strong>admin / 123456</p>
                  <p><strong>用户：</strong>user / 123456</p>
                </div>
              }
              type="info"
              showIcon
            />
          </div>

          <div className="login-footer">
            <div className="system-info">
              <span className="info-item">
                <WindowsOutlined /> React 19
              </span>
              <span className="info-item">
                <LineChartOutlined /> Ant Design
              </span>
              <span className="info-item">
                <StarFilled />现代化管理
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

export default memo(Login);
